package cn.snowsoft.iot.module.warning.service.warning.record;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.snowsoft.iot.framework.common.pojo.CommonResult;
import cn.snowsoft.iot.module.cps.api.equipment.AdminEquipmentApi;
import cn.snowsoft.iot.module.cps.api.equipment.AdminPointApi;
import cn.snowsoft.iot.module.cps.api.equipment.dto.EquipmentDTO;
import cn.snowsoft.iot.module.cps.api.equipment.dto.EquipmentPointDTO;
import cn.snowsoft.iot.module.message.api.message_record.MessageRecordApi;
import cn.snowsoft.iot.module.message.api.message_record.dto.MessageRecordRequestDto;
import cn.snowsoft.iot.module.message.api.message_record.dto.MessageRecordRespDto;
import cn.snowsoft.iot.module.warning.controller.admin.home.vo.RecordVO;
import cn.snowsoft.iot.module.warning.controller.admin.home.vo.WarningEquipmentVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.record.vo.WarningRecordDetailReqVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.record.vo.WarningRecordDetailRespVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.record.vo.WarningRecordPageVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.record.vo.WarningRecordStatusVO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.config.WarningDO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.record.OutPutRecord;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.record.WarningRecordDO;
import cn.snowsoft.iot.module.warning.dal.mysql.warning.record.WarningRecordMapper;
import cn.snowsoft.iot.module.warning.enums.WarningRecord;
import cn.snowsoft.iot.module.warning.service.warning.config.IWarningService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WarningRecordServiceImpl extends ServiceImpl<WarningRecordMapper, WarningRecordDO> implements IWarningRecordService {

    @Resource
    private IWarningService warningService;

    @Resource
    private MessageRecordApi messageRecordApi;
    @Resource
    private IOutPutRecordService outPutRecordService;



    @Override
    public IPage<WarningRecordDO> selectRecordPage(WarningRecordPageVO recordPageVO) {
        IPage<WarningRecordDO> page = new Page<>(recordPageVO.getPageNo(), recordPageVO.getPageSize());
        return baseMapper.selectWarningPage(page, recordPageVO);
    }

    @Override
    public CommonResult<WarningRecordDO> updateStatus(WarningRecordStatusVO recordStatusVO) {
        if (recordStatusVO.getId() != null) {
            WarningRecordDO warningRecordDO = new WarningRecordDO();
            warningRecordDO.setId(recordStatusVO.getId());
            if (recordStatusVO.getStatus() != null) {
                warningRecordDO.setStatus(recordStatusVO.getStatus());
                warningRecordDO.setHandleUser(recordStatusVO.getHandleUser());
                LocalDateTime now = LocalDateTime.now();
                warningRecordDO.setHandleTime(now);
                warningRecordDO.setRemark(recordStatusVO.getRemark());
                updateById(warningRecordDO);
                return CommonResult.success(warningRecordDO);
            } else {
                return CommonResult.error(400, "参数错误");
            }
        } else {
            return CommonResult.error(400, "参数错误");
        }
    }

    public void handleWarningRecord(JSONObject jsonObject, String warningType) {
        WarningRecordDO warningRecordDO = new WarningRecordDO();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        Long configId = jsonObject.getLong("warningId");
        String warningTime = jsonObject.getString("thetime");
        String equipmentName = jsonObject.getString("deviceName");
        String equipmentCode = jsonObject.getString("deviceCode");
        String uuId = jsonObject.getString("uuId");
        if (ObjectUtil.isEmpty(warningTime)) {
            warningTime = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS");
        }
        WarningDO warningDO = "1".equals(warningType) ? warningService.getByRuleId(configId) : warningService.selectWarningByWarningId(configId);
        if (!warningTime.matches(".*\\.\\d{1,3}$")) {
            warningTime += ".000";
        }
        LocalDateTime dateTime = LocalDateTime.parse(warningTime, formatter);
        warningRecordDO.setWarningTag(uuId);
        warningRecordDO.setConfigId(warningDO.getId());
        warningRecordDO.setConfigName(warningDO.getWarningName());
        warningRecordDO.setWarningData(String.valueOf(jsonObject));
        warningRecordDO.setEquipmentName(equipmentName);
        warningRecordDO.setEquipmentCode(equipmentCode);
        warningRecordDO.setWarningTime(dateTime);
        warningRecordDO.setStatus(WarningRecord.DISABLE);
        warningRecordDO.setWarningType(warningType);
        save(warningRecordDO);
    }


    @Override
    public List<RecordVO> getRecordList() {
        return baseMapper.getRecordList();
    }

    @Override
    public List<WarningEquipmentVO> getWarningEquipmentList() {
        return baseMapper.getWarningEquipmentList();
    }

    //查询告警记录动作详情
    @Override
    public List getRecodeActionDetailList(WarningRecordDetailReqVO warningRecordDetailReqVO) {

        List<WarningRecordDetailRespVO> warningRecordDetailRespVO = baseMapper.getRecordActionDetailList(warningRecordDetailReqVO);

        for (WarningRecordDetailRespVO recordDetailRespVO : warningRecordDetailRespVO) {
            Integer failCount = 0;
            Integer successCount = 0;
            MessageRecordRequestDto messageRecordRequestDto = new MessageRecordRequestDto();
            messageRecordRequestDto.setWarningRecordId(warningRecordDetailReqVO.getWarningTag());
            List messageRecordDetail = messageRecordApi.getMessageRecordDetail(messageRecordRequestDto);

            for (int i = 0; i < messageRecordDetail.size(); i++) {
                MessageRecordRespDto messageRecordRespDto = (MessageRecordRespDto) messageRecordDetail.get(i);
                if (messageRecordRespDto.getMessageStatus()==0){
                    failCount++;
                }else{
                    successCount++;
                }
            }
            recordDetailRespVO.setSuccessCount(successCount);
            recordDetailRespVO.setFailCount(failCount);
            recordDetailRespVO.setDataList(messageRecordDetail);

        }
        return warningRecordDetailRespVO;
    }

    public CommonResult<WarningRecordDO> test(String data) {
        WarningRecordDO warningRecordDO = new WarningRecordDO();
        JSONObject jsonObject =  JSON.parseObject(data);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        Long configId = (Long) jsonObject.get("warningId");
        String warningTime = (String) jsonObject.get("thetime");
        WarningDO warningDO = warningService.getById(configId);
        LocalDateTime dateTime = LocalDateTime.parse(warningTime, formatter);

        warningRecordDO.setConfigId(warningDO.getId());
        warningRecordDO.setConfigName(warningDO.getWarningName());
        warningRecordDO.setWarningData(String.valueOf(jsonObject));
        warningRecordDO.setWarningTime(dateTime);
        warningRecordDO.setStatus(WarningRecord.DISABLE);

        save(warningRecordDO);
        return CommonResult.success(warningRecordDO);
    }
}
